package red.dao.impl;


import red.dao.ColumnDao;
import red.model.ColumnModel;
import red.util.MysqlConnectUtils;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ColumnDaoImpl implements ColumnDao {

    private MysqlConnectUtils mysqlconnectutils = new MysqlConnectUtils();

    @Override
    public ColumnModel getColumn(Integer uid) {
        return null;
    }

    @Override
    public List<ColumnModel> getColumn() {
        return null;
    }

//添加
    @Override
    public ColumnModel saveColumn(ColumnModel columnModel) {
        String column_name = columnModel.getColumn_name();
        String column_id = columnModel.getColumn_id();
        String sql = "insert into sys_column(column_id,column_name,column_date) "
                + "value('"+column_id+"','"+column_name+"',now())";
        mysqlconnectutils.insert(sql);
        return columnModel;
    }
//删除
    @Override
    public ColumnModel deleteColumn(ColumnModel columnModel) {
        String column_name = columnModel.getColumn_name();
        String sql = "delete from sys_column where column_name= "
                + "'"+column_name+ "'";
        mysqlconnectutils.delete(sql);
        return columnModel;
    }
//查询所有
    @Override
    public List<ColumnModel> selectColumn(ColumnModel columnModel) {
        String sql = "select * from sys_column ";
        List<ColumnModel> models =new ArrayList<>();
        List<Map<String, Object>> list = mysqlconnectutils.list(sql);
        for(int i=0;i<list.size();i++){
            Map<String, Object> row = list.get(i);
            ColumnModel model = new ColumnModel();
            model.setColumn_name(row.get("column_name").toString());
            model.setColumn_id(row.get("column_id").toString());
            model.setColumn_date(row.get("column_date").toString());
            models.add(model);
        }
        return models;
    }

    @Override
    public ColumnModel updateColumn(ColumnModel columnModel) {
        return columnModel;
    }

}
